<template>
  <el-dialog
    :model-value="showAddVideoVisible"
    :title="props.editInfo.id ? '编辑视频' : '新增短视频'"
    width="700"
    :close-on-click-modal="false"
    @close="resetForm"
  >
    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-width="auto">
      <el-form-item label="排序" prop="sortOrder">
        <el-input v-model="ruleForm.sortOrder" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="短视频标题" prop="title">
        <el-input v-model="ruleForm.title" type="textarea" :rows="6" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="标签">
        <el-input
          v-model="ruleForm.tags"
          type="textarea"
          :rows="6"
          placeholder="多个标签请用英文逗号分割"
        />
      </el-form-item>
      <el-form-item label="视频封面" prop="coverUrl">
        <el-input v-model="ruleForm.coverUrl" style="display: none"></el-input>
        <div class="avatar-uploader" @click="showImageChooseDialog = true">
          <el-image
            v-if="ruleForm.coverUrl"
            :src="ruleForm.coverUrl"
            class="avatar"
            fit="contain"
          ></el-image>
          <i v-else class="el-icon-plus avatar-uploader-icon"
            ><el-icon> <Plus /> </el-icon
          ></i>
          <div v-if="ruleForm.coverUrl" class="mask">替换</div>
          <el-icon v-if="ruleForm.coverUrl" class="del-image" :size="20" @click.stop="handleDelImg">
            <CircleCloseFilled />
          </el-icon>
        </div>
      </el-form-item>

      <el-form-item label="视频" prop="videoUrl">
        <el-input v-model="ruleForm.videoUrl">
          <template #append>
            <div class="video-uploader" @click="showVideoChooseDialog = true">
              <el-button style="color: #2b8dfa" type="primary">选择视频</el-button>
            </div>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="用户权限" prop="requireVip">
        <div>
          <el-radio-group v-model="ruleForm.requireVip">
            <el-radio :value="0">全部用户可看</el-radio>
            <el-radio :value="1">仅限VIP会员可看</el-radio>
          </el-radio-group>
          <div style="color: #ccc; font-size: 12px">选择“仅限VIP会员可看”时请先设置好会员权益</div>
        </div>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-switch v-model="ruleForm.status" :active-value="1" :inactive-value="0"></el-switch>
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" :loading="editLoading" @click="submitForm(ruleFormRef)"
          >确认</el-button
        >
        <el-button @click="resetForm">取消</el-button>
      </div>
    </template>
  </el-dialog>
  <image-selected-dialog
    v-if="showImageChooseDialog"
    :single-type="selectImageSingle"
    :show-image-choose-dialog="showImageChooseDialog"
    @close-dialog="handleCloseImageDialog"
    :operationType="false"
  ></image-selected-dialog>
  <video-group-dialog
    v-if="showVideoChooseDialog"
    :show-video-choose-dialog="showVideoChooseDialog"
    :single-type="singleType"
    :operation-type="false"
    @close-dialog="handleCloseVideoDialog"
  />
</template>

<script setup lang="ts">
  import { reactive, ref } from 'vue'
  import { ElMessage, FormInstance, FormRules } from 'element-plus'
  import { Plus } from '@element-plus/icons-vue'
  import ImageSelectedDialog from '@/views/decoration/diyComponents/ImageSelectedDialog.vue'
  import VideoGroupDialog from '@/views/decoration/diyComponents/VideoGroupDialog.vue'
  import { addVideo, updateVideo } from '@/api/marketing'

  const props = defineProps({
    showAddVideoVisible: {
      type: Boolean
    },
    editInfo: {
      type: Object,
      default: () => {
        return {
          id: ''
        }
      }
    },
    groupInfo: {
      type: Object,
      default: () => {
        return {
          id: ''
        }
      }
    }
  })

  const emit = defineEmits(['closeDialog'])
  const editLoading = ref(false)

  interface RuleForm {
    sortOrder: string
    title: string
    tags: string
    status: number
    requireVip: number
    introduction: string
    coverUrl: string
    videoUrl: string
  }

  const ruleFormRef = ref<FormInstance>()

  const ruleForm = reactive<RuleForm>({
    sortOrder: '1',
    title: '',
    tags: '',
    status: 1,
    requireVip: 0,
    introduction: '',
    coverUrl: '',
    videoUrl: ''
  })

  const showImageChooseDialog = ref(false)
  const selectImageSingle = ref(true)
  const singleType = ref(true)
  const showVideoChooseDialog = ref(false)
  const handleCloseImageDialog = (value: any) => {
    if (value && value.fileUrl) {
      ruleForm.coverUrl = value.fileUrl
    }
    showImageChooseDialog.value = false
  }
  const handleCloseVideoDialog = (value: any) => {
    if (value && value.fileUrl) {
      ruleForm.videoUrl = value.fileUrl
    }
    showVideoChooseDialog.value = false
  }

  const handleDelImg = () => {
    ruleForm.coverUrl = ''
  }

  const rules = reactive<FormRules<RuleForm>>({
    sortOrder: [{ required: true, message: '不能为空', trigger: 'blur' }],
    title: [{ required: true, message: '不能为空', trigger: 'blur' }],
    coverUrl: [{ required: true, message: '不能为空', trigger: 'blur' }],
    videoUrl: [{ required: true, message: '不能为空', trigger: 'blur' }],
    requireVip: [{ required: true, message: '不能为空', trigger: 'blur' }],
    status: [{ required: true, message: '不能为空', trigger: 'blur' }]
  })

  const submitForm = async (formEl: FormInstance | undefined) => {
    if (!formEl) return
    await formEl.validate((valid) => {
      if (valid) {
        if (props.editInfo.id) {
          handleEdit()
        } else {
          handleAdd()
        }
      } else {
      }
    })
  }

  async function handleAdd() {
    try {
      editLoading.value = true
      const res = await addVideo({
        ...ruleForm,
        creatorId: props.groupInfo.id
      })
      if (res.code === '00000') {
        emit('closeDialog', true)
        ElMessage.success('新增成功')
      }
    } catch (e) {
    } finally {
      editLoading.value = false
    }
  }

  async function handleEdit() {
    try {
      editLoading.value = true
      const res = await updateVideo(props.editInfo.id, {
        ...ruleForm,
        creatorId: props.groupInfo.id
      })
      if (res.code === '00000') {
        emit('closeDialog', true)
        ElMessage.success('编辑成功')
      }
    } catch (e) {
    } finally {
      editLoading.value = false
    }
  }

  const resetForm = () => {
    emit('closeDialog')
  }
  onMounted(() => {
    if (props.editInfo.id) {
      ruleForm.sortOrder = props.editInfo.sortOrder
      ruleForm.title = props.editInfo.title
      ruleForm.tags = props.editInfo.tags
      ruleForm.status = props.editInfo.status
      ruleForm.requireVip = props.editInfo.requireVip
      ruleForm.videoUrl = props.editInfo.videoUrl
      ruleForm.coverUrl = props.editInfo.coverUrl
    }
  })
</script>

<style scoped lang="scss">
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;

    .mask {
      position: absolute;
      bottom: 0;
      left: 0;
      width: 100%;
      height: 24px;
      background-color: rgba(0, 0, 0, 0.6);
      display: flex;
      align-items: center;
      justify-content: center;
      opacity: 0;
      transition: opacity 0.3s ease;
      color: #fff;
      font-size: 12px;
    }

    .del-image {
      display: none;
      position: absolute;
      top: 0;
      right: 0;
      cursor: pointer;
      color: #8c939d;
    }

    &:hover .mask {
      opacity: 1;
    }

    &:hover .del-image {
      display: block;
    }
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409eff;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
